package com.yang.controller;

import com.github.pagehelper.PageInfo;
import com.yang.pojo.Bill;
import com.yang.pojo.User;
import com.yang.service.BillService;
import com.yang.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/sys/bill")
public class BillController {
    @Autowired
    private BillService billService;
    /**
     * 前往订单信息页面
     * @param bill
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping(value = "toList")
    public String toList(Bill bill, Integer pageNum, Integer pageSize, Model model) {
        if (null == pageNum) {
            pageNum=1;
        }
        if (null == pageSize) {
            pageSize=5;
        }
        PageInfo<Bill> pageInfo = billService.queryByBill(bill, pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("bill",bill);
        return "bill_list";
    }

    /**
     * 前往添加订单页面进行添加
     * @return
     */
    @RequestMapping(value="/toSave",method = RequestMethod.GET)
    public String toSave(){
        return "bill_save";
    }

    @ResponseBody
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(Bill bill, HttpSession session){
        User loginUser = (User) session.getAttribute(Common.LOGIN_USER);
        bill.setCreatedBy(loginUser.getId());
        Integer count = billService.save(bill);
        if (count>0){
            return "success";
        }
        return "error";
    }

    /**
     * 前往订单信息查看页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value="/toDetail",method = RequestMethod.GET)
    public String toDetail(Model model,Integer id){
        Bill bill = billService.queryById(id);
        model.addAttribute("bill",bill);
        return "bill_detail";
    }
    //没有找到前端资源，暂时没有完善
//    @RequestMapping()
//    public String updateBill(){
//        return "";
//    }
    /**
     * 删除订单
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(Integer id){
        Integer count = billService.delete(id);
        if (count>0){
            return "success";
        }
        return "error";
    }
}
